#!/usr/bin/python
# -*- coding: utf-8 -*-

import wx
from dyn_plot import DynPlot, RealSettings, RealKoeffs, Curve


class GraphWindow(wx.Window):

    def __init__(self, parent):
        wx.Window.__init__(self, parent)
        self.parent_pause = parent.on_pause

        self.lastTime = 500
        self.masses = [[50],[100],[150]]
        realSettings = RealSettings(yRealMax = 288, yRealMin = -72, xRealWidth = 400,
                                    xRealStep = 50, yRealStep = 50)
        colors = [(0x55, 0x55, 0xFF), (0xFF, 0x55, 0xFF), (0x55, 0x55, 0x55)]
        names = [u"Синий график", u"Красный график", u"Серый график"]
        curves = [Curve(mass, color, name) for (mass, color, name) in zip(self.masses, colors, names)]

        self.graph = DynPlot(realSettings, RealKoeffs(0.5, 1.0), curves)
        self.graph.legends_visible(True)

        self.init_buffer()
        self.Bind(wx.EVT_SIZE, self.on_size)
        self.Bind(wx.EVT_PAINT, self.on_paint)
        self.Bind(wx.EVT_KEY_DOWN, self.on_key_down)

    def on_size(self, evt):
        # When the window size changes we need a new buffer.
        self.init_buffer()

    def on_paint(self, evt):
        dc = wx.PaintDC(self)
        w, h = self.memoryDC.GetSizeTuple()
        dc.Blit(0, 0, w, h, self.memoryDC, 0, 0)

    def init_buffer(self):
        w, h = self.GetClientSize()
        self.buffer = wx.EmptyBitmap(w, h)
        self.memoryDC = wx.MemoryDC(self.buffer)
        self.graph.draw(self.memoryDC, self.lastTime)
        self.Refresh(False)

    def set_data(self):
        import random

        for m in self.masses:
            m.append(m[-1] + random.randint(-5, 5))

        self.lastTime += 2
        self.graph.set_index(len(self.masses[0]))
        self.graph.draw(self.memoryDC, self.lastTime)
        self.Refresh(False)

    def set_index(self, index):
        self.graph.set_index(index)
        self.graph.draw(self.memoryDC, self.lastTime)
        self.Refresh(False)

    def get_pos(self):
        return len(self.masses[0])

    def on_key_down(self, evt):
        if evt.GetKeyCode() == wx.WXK_SPACE:
            self.parent_pause()

        if evt.GetKeyCode() == wx.WXK_F1:
            pos = evt.GetPosition()
            self.graph.scale(2, pos)

        if evt.GetKeyCode() == wx.WXK_F2:
            pos = evt.GetPosition()
            self.graph.scale(0.5, pos)

        self.graph.draw(self.memoryDC, self.lastTime)
        self.Refresh(False)

class TestFrame(wx.Frame):

    def __init__(self):

        wx.Frame.__init__(self, parent=None, title="Plot", size=(480,480))

        # Create a timer to update the data values
        self.timer = wx.Timer(self)
        self.timer.Start(200)
        self.Bind(wx.EVT_TIMER, self.on_timeout)

        self.plot = GraphWindow(self)
        self.scroll = wx.ScrollBar(self, -1)

        sizer = wx.BoxSizer(wx.VERTICAL)
        self.Bind(wx.EVT_SCROLL, self.on_scroll)
        self.Bind(wx.EVT_SCROLL_LINEDOWN, self.on_linedown)
        self.Bind(wx.EVT_SCROLL_LINEUP, self.on_lineup)
        self.Bind(wx.EVT_SCROLL_THUMBTRACK, self.on_thumbtrack)

        sizer.Add(self.plot, proportion=1, flag=wx.ALIGN_CENTER | wx.EXPAND)
        sizer.Add(self.scroll, flag=wx.ALIGN_BOTTOM | wx.EXPAND)

        self.SetSizer(sizer)
        self.Layout()

    def on_scroll(self, evt):
        self.plot.SetFocus()

    def on_linedown(self, evt):
        pos = self.scroll.GetThumbPosition()
        self.plot.set_index(pos)

    def on_lineup(self, evt):
        pos = self.scroll.GetThumbPosition()
        self.plot.set_index(pos)

    def on_thumbtrack(self, evt):
        pos = self.scroll.GetThumbPosition()
        self.plot.set_index(pos)

    def on_timeout(self, evt):
        self.plot.set_data()
        data = position, thumbSize, range, pageSize, refresh = (
            self.plot.get_pos(), 0, self.plot.get_pos(), 10, True)
        self.scroll.SetScrollbar(*data)

    def on_pause(self):
        if self.timer.IsRunning():
            self.timer.Stop()
        else:
            self.timer.Start(200)


def main():
    app = wx.PySimpleApp()
    frm = TestFrame()
    frm.Show()
    app.MainLoop()


if __name__ == "__main__":
    main()


